package com.doit.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class _11_get数据 {
    public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        //设置参数
        conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
        //创建连接对象
        Connection conn = ConnectionFactory.createConnection(conf);
        //获取连接对象
        Admin admin = conn.getAdmin();

        //table对象，这个对象就是用来对于表里面数据的操作
        Table table = conn.getTable(TableName.valueOf("test:teacher"));

        //获取的一行数据
        Get get = new Get("rowkey002".getBytes(StandardCharsets.UTF_8));
        get.addFamily("base_info".getBytes(StandardCharsets.UTF_8));
        get.addColumn("base_info".getBytes(StandardCharsets.UTF_8),"age".getBytes(StandardCharsets.UTF_8));


        Get get1 = new Get("rowkey005".getBytes(StandardCharsets.UTF_8));
        get1.addFamily("base_info".getBytes(StandardCharsets.UTF_8));
        get1.addColumn("base_info".getBytes(StandardCharsets.UTF_8),"name".getBytes(StandardCharsets.UTF_8));

        ArrayList<Get> gets = new ArrayList<>();
        gets.add(get);
        gets.add(get1);

        Result[] results = table.get(gets);
        //相当于是一个迭代器  hasnext   next
        for (Result result : results) {
            while (result.advance()){
                Cell current = result.current();
                String family = new String(CellUtil.cloneFamily(current));
                String rowKey = new String(CellUtil.cloneRow(current));
                String qualifier = new String(CellUtil.cloneQualifier(current));
                String value = new String(CellUtil.cloneValue(current));
                System.out.println(rowKey+","+family+","+qualifier+","+value);
            }
        }



    }
}
